package com.itWaiLong.Service.Impl;

import com.itWaiLong.AppException.AppException;
import com.itWaiLong.AppException.GlobalExceptionHandler;
import com.itWaiLong.Dao.Entities.addPostEntities;
import com.itWaiLong.Dao.tech_discussion;
import com.itWaiLong.Mapper.PostMapper;
import com.itWaiLong.Service.PostService;
import com.itWaiLong.Utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.Map;

@Service
public class PostServiceImpl implements PostService {

    @Autowired
    private PostMapper postMapper;

    /**
     * 添加讨论主题
     * @param addPostEntities
     * @return
     */
    public void add(addPostEntities addPostEntities) {

        tech_discussion techDiscussion = new tech_discussion();
        techDiscussion.setTopic(addPostEntities.getTopic());
        techDiscussion.setContent(addPostEntities.getContent());

        //设置添加时间
        techDiscussion.setCreationTime(LocalDate.now());

        //获取用户id
        Map<String, Object> map = ThreadLocalUtil.getThreadLocal();
        int  userId = (int) map.get("userId");
        //设置techDiscussion中的用户id
        techDiscussion.setUserId(userId);
        int result = postMapper.add(techDiscussion);
        if (result < 0 ) {
            throw new GlobalExceptionHandler(AppException.TIAN_JIA_CHENG_GONG);
        }
    }

    /**
     * 删除讨论
     * @param postId
     * @return
     */
    public void delete(int postId) {
        int result = postMapper.delete(postId);
        if (result < 0) {
            throw new GlobalExceptionHandler(AppException.SHAN_CHU_SHI_BAI);
        }
    }
}
